package com.itheima.utils;

import com.alibaba.fastjson.JSON;

import io.jsonwebtoken.*;
import org.springframework.beans.factory.annotation.Value;

import java.util.Map;

/**
 * 解析token
 */
public class TokenDecode {

    @Value("${sign}")
    private String sign;


    /***
     * 读取令牌数据
     */
    public Map<String, String> decodeToken(String token) {
        try {
            //获取Jwt原始内容
            Claims claims = Jwts.parser().setSigningKey(sign).parseClaimsJws(token).getBody();
            Map<String, String> map = JSON.parseObject(JSON.toJSONString(claims), Map.class);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("token不存在或过期!");
        }
    }
}
